package com.lill.system.serivce.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.util.IdUtil;
import com.lill.common.component.util.StringUtils;
import com.lill.system.constant.SystemConstant;
import com.lill.system.dao.ISysUserDao;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysRoleService;
import com.lill.system.service.ISysUserAppService;
import com.lill.system.service.ISysUserRoleService;
import com.lill.system.service.ISysUserService;


/**
 * 系统用户
 *
 * @author lill
 */
@Service("sysUserService")
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private ISysUserDao sysUserDao;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysUserAppService sysUserAppService;
    @Autowired
    private ISysRoleService sysRoleService;

    public List<String> queryAllPerms(String userId) {
        return sysUserDao.queryAllPerms(userId);
    }

    public List<String> queryAllMenuId(String userId) {
        return sysUserDao.queryAllMenuId(userId);
    }

    public SysUserEntity queryByUserName(String username) {
        return sysUserDao.queryByUserName(username);
    }

    public SysUserEntity queryObject(String userId) {
        return sysUserDao.queryObject(userId);
    }

    public List<SysUserEntity> queryList(Map<String, Object> map) {
        return sysUserDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return sysUserDao.queryTotal(map);
    }

    @Transactional
    public int save(SysUserEntity user) {
        user.setId(IdUtil.createIdbyUUID());
        //sha256加密
        user.setPassWord(new Sha256Hash(SystemConstant.DEFAULT_OLD_PASS_WORD).toHex());
        int result=sysUserDao.save(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
        
//        sysUserAppService.saveOrUpdate(user.getId(), user.getAppIdList());
        return result;
    }

    public int updateApp(SysUserEntity user){
        return sysUserDao.updateApp(user);
    }

    @Transactional
    public int update(SysUserEntity user,SysUserEntity curUser) {
        if (StringUtils.isBlank(user.getPassWord())) {
            user.setPassWord(new Sha256Hash(SystemConstant.DEFAULT_OLD_PASS_WORD).toHex());
        } else {
            user.setPassWord(new Sha256Hash(user.getPassWord()).toHex());
        }
        int result=sysUserDao.update(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
        
//        sysUserAppService.saveOrUpdate(user.getId(), user.getAppIdList());
        return result;
    }
    @Transactional
    public int updates(SysUserEntity user,SysUserEntity curUser) {
        if (StringUtils.isBlank(user.getPassWord())) {
            user.setPassWord(new Sha256Hash(SystemConstant.DEFAULT_OLD_PASS_WORD).toHex());
        } else {
            user.setPassWord(new Sha256Hash(user.getPassWord()).toHex());
        }
        int result=sysUserDao.update(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdates(user.getId(), user.getRoleIdList());

//        sysUserAppService.saveOrUpdate(user.getId(), user.getAppIdList());
        return result;
    }

    @Transactional
    public int deleteBatch(String[] userId) {
        return sysUserDao.deleteBatch(userId);
    }

    public int updatePassword(String userId, String password, String newPassword) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassword);
        return sysUserDao.updatePassword(map);
    }

    /**
     * 检查角色是否越权
     */
//    private void checkRole(SysUserEntity user) {
//        //如果不是超级管理员，则需要判断用户的角色是否自己创建
//        if (SystemConstant.SUPER_ADMIN.equals(user.getCreateUser())) {
//            return;
//        }
//
//        //查询用户创建的角色列表
//        List<String> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUser());
//
//        //判断是否越权
//        if (!roleIdList.containsAll(user.getRoleIdList())) {
//            throw new RuntimeException("新增用户所选角色，不是本人创建");
//        }
//    }

	@Override
	public int initPassword(String userId, String newPassword) {
		// initPassword
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", userId);
        map.put("newPassword", newPassword);
        return sysUserDao.initPassword(map);
	}
	

	@Override
	public int deleteLogicByMap(Map<String, Object> params) {
		return sysUserDao.deleteLogicByMap(params);
	}

    public SysUserEntity queryPhone(String phone,String domainCode){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("phone",phone);
        map.put("domainCode",domainCode);
        List<SysUserEntity> users = sysUserDao.queryPhone(map);
        if(users == null || users.size() <= 0){
            return null;
        }
        return users.get(0);
    }

    @Override
    public void update(SysUserEntity user) {
        sysUserDao.update(user);
    }

    @Override
    public List<SysUserEntity> queryAll(Map<String, Object> map) {
        return sysUserDao.queryAll(map);
    }

	public SysUserEntity queryPhone(String phone) {
		return null;
	}

	@Override
	public SysUserEntity queryByPersonId(String personId) {
		return sysUserDao.queryByPersonId(personId);
	}

	@Override
	public List<Map<String, Object>> queryUserLogin(Map<String, Object> param) {
		return sysUserDao.queryUserLogin(param);
	}

    @Override
    public int updateByPhone(SysUserEntity user) {
        //保存用户与角色关系
        sysUserRoleService.saveOrUpdates(user.getId(), user.getRoleIdList());
        return sysUserDao.updateByPhone(user);
    }

}
